/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.config.VideoInfoBean;
import com.yc.video.player.VideoPlayer;
import com.yc.video.ui.view.BasisVideoController;
import com.yc.video.ui.view.CustomBottomView;

import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.clarity.DefinitionControlView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 11.1 视频播放器切换清晰度操作
 *
 * @since 2021-05-10
 */
public class ClarityAbility extends BaseAbility implements Component.ClickedListener {
    private VideoPlayer mVideoPlayer;
    private Button mBtnScaleNormal;
    private Button mBtnScale169;
    private Button mBtnScale43;
    private Button mBtnCrop;
    private Button mBtnGif;

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_normal_video;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);
        mVideoPlayer = (VideoPlayer) findComponentById(ResourceTable.Id_video_player);
        mBtnScaleNormal = (Button) findComponentById(ResourceTable.Id_btn_scale_normal);
        mBtnScale169 = (Button) findComponentById(ResourceTable.Id_btn_scale_169);
        mBtnScale43 = (Button) findComponentById(ResourceTable.Id_btn_scale_43);
        mBtnCrop = (Button) findComponentById(ResourceTable.Id_btn_crop);
        mBtnGif = (Button) findComponentById(ResourceTable.Id_btn_gif);
        initVideoPlayer();
    }

    private void initVideoPlayer() {
        mController = new BasisVideoController(this);
        CustomBottomView bottomView = mController.getBottomView();
        if (bottomView != null) {
            mController.removeControlComponent(bottomView);
        }
        DefinitionControlView mDefinitionControlView = new DefinitionControlView(this);
        ComponentContainer.LayoutConfig layoutConfig = mDefinitionControlView.getLayoutConfig();
        layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = ComponentContainer.LayoutConfig.MATCH_PARENT;
        mDefinitionControlView.setOnRateSwitchListener(new DefinitionControlView.OnRateSwitchListener() {
            @Override
            public void onRateChange(String url) {
                mVideoPlayer.setUrl(url);
                mVideoPlayer.replay(false);
            }
        });
        List<VideoInfoBean> clarites = getClarites();
        LinkedHashMap<String, String> videos = new LinkedHashMap<>();
        for (int i = 0; i < clarites.size(); i++) {
            VideoInfoBean videoClarity = clarites.get(i);
            videos.put(videoClarity.getGrade(), videoClarity.getVideoUrl());
        }
        mDefinitionControlView.setData(videos);
        mController.addControlComponent(mDefinitionControlView);

        // 设置视频背景图
        mController.getThumb().setPixelMap(ResourceTable.Media_image_default);

        // 设置控制器
        mVideoPlayer.setController(mController);
        mVideoPlayer.setUrl(clarites.get(0).getVideoUrl());
        mVideoPlayer.start();
    }

    /**
     * 获得 Clarites
     *
     * @return 视频信息
     */
    public List<VideoInfoBean> getClarites() {
        List<VideoInfoBean> clarities = new ArrayList<>();
        clarities.add(new VideoInfoBean("标题哈哈哈哈", "标清", "270P", "http://play.g3proxy.lecloud.com/vod/v2/MjUxLzE2LzgvbGV0di11dHMvMTQvdmVyXzAwXzIyLTExMDc2NDEzODctYXZjLTE5OTgxOS1hYWMtNDgwMDAtNTI2MTEwLTE3MDg3NjEzLWY1OGY2YzM1NjkwZTA2ZGFmYjg2MTVlYzc5MjEyZjU4LTE0OTg1NTc2ODY4MjMubXA0?b=259&mmsid=65565355&tm=1499247143&key=f0eadb4f30c404d49ff8ebad673d3742&platid=3&splatid=345&playid=0&tss=no&vtype=21&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
        clarities.add(new VideoInfoBean("标题哈哈哈哈", "高清", "480P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
        clarities.add(new VideoInfoBean("标题哈哈哈哈", "超清", "720P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
        clarities.add(new VideoInfoBean("标题哈哈哈哈", "蓝光", "1080P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
        return clarities;
    }

    @Override
    protected void initListener() {
        super.initListener();
        mBtnScaleNormal.setClickedListener(this);
        mBtnScale169.setClickedListener(this);
        mBtnScale43.setClickedListener(this);
        mBtnCrop.setClickedListener(this);
        mBtnGif.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        if (component == mBtnScale169) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_16_9);
        } else if (component == mBtnScaleNormal) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_DEFAULT);
        } else if (component == mBtnScale43) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_4_3);
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (mVideoPlayer != null) {
            // 从后台切换到前台，当视频暂停时或者缓冲暂停时，调用该方法重新开启视频播放
            mVideoPlayer.resume();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (mVideoPlayer != null) {
            // 从前台切到后台，当视频正在播放或者正在缓冲时，调用该方法暂停视频
            mVideoPlayer.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoPlayer != null) {
            // 销毁页面，释放，内部的播放器被释放掉，同时如果在全屏、小窗口模式下都会退出
            mVideoPlayer.release();
        }
    }

    @Override
    protected void onBackPressed() {
        ControlWrapper.defaultValue();
        // 处理返回键逻辑；如果是全屏，则退出全屏；如果是小窗口，则退出小窗口
        if (mVideoPlayer == null || !mVideoPlayer.onBackPressed()) {
            super.onBackPressed();
        }
    }
}
